//
//  DoubleDetailViewController.m
//  MINFOBBS
//
//  Created by 蔡龙生 on 13-11-28.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "DoubleDetailViewController.h"
//#import "SBJsonParser.h"
#import "DoubleBallCell.h"
#import "DetectionSystemVersion.h"
#import "AwardLevelCell.h"
#import "WebSaleCaiController.h"
@interface DoubleDetailViewController ()

@end

@implementation DoubleDetailViewController
@synthesize doubleBalls;
- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    [self BackButton];
//    [ self GetWeatherSearch:self.query Location:self.city];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
	// Do any additional setup after loading the view.
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
//#pragma mark - Get Lottery Ticket Data
//-(void)GetWeatherSearch:(NSString*)Query Location:(NSString*)City{
//     tRequest=tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:Query Start:0 Limit:10];  
//    [tRequest setDelegate:self];
//    [tRequest setUsername:@"doubleColorBall"];
//}
//
//-(void)requestFinished:(ASIHTTPRequest*)request{
//    NSString *response = [request responseString];
//    SBJsonParser * parser = [[SBJsonParser alloc] init];
//    NSMutableDictionary *Result = [parser objectWithString:response];
//    
//    if ([request.username isEqual:@"doubleColorBall"]){
//        BOOL IsSuccess=[[[Result objectForKey:@"Response"]objectForKey:@"status"]integerValue]==200?YES:NO;
//        if (IsSuccess==YES) {
//            NSMutableArray *arrReturn=[[NSMutableArray alloc] init];
//            [arrReturn addObject:[[Result objectForKey:@"Response"] objectForKey:@"Categories"]];
//            for (int i=0; i<arrReturn.count; i++) {
//                NSArray *arr=[[arrReturn objectAtIndex:i] objectForKey:@"Category"];
//                for (int j=0; j<arr.count; j++) {
//                    if([[[arr objectAtIndex:j] objectForKey:@"Entrance"] isEqual:@"Lotto.GetLotto"])
//                    {
//                        NSArray *itemReturn=[[NSArray alloc] init];
//                        itemReturn=[[[arr objectAtIndex:j] objectForKey:@"Items"] objectForKey:@"Item"];
//                        for (int k=0; k<itemReturn.count; k++) {
//                            if(doubleBalls==nil){
//                                doubleBalls=[[NSMutableArray alloc] init];
//                            }
//                            [doubleBalls addObject:[itemReturn objectAtIndex:k]];
//                        }
//                    }
//                }
//            }
//        }
//        
//        [self.tableView reloadData];
//    }
//}
//
//-(void)requestFailed:(ASIHTTPRequest*)request{
//    
//}
//
#pragma mark - Table View DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *list=[[[doubleBalls objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"];
    NSString *awards=[[list objectAtIndex:self.issueIndex*7+8] objectForKey:@"text"];
    NSArray *awardItems=[awards componentsSeparatedByString:@"><"];
    if(awardItems.count>0){
        return awardItems.count+2;
    }
    else{
        return 0;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *list=[[[doubleBalls objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"];
    NSUInteger row=[indexPath row];
    
    if (row==0) {
        static NSString *CustomCellIdentifier = @"DoubleBallCell";
        DoubleBallCell *cell=(DoubleBallCell *) [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        if(cell==nil){
            cell=[[[NSBundle mainBundle] loadNibNamed:CustomCellIdentifier owner:self options:nil] objectAtIndex:0];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
        cell.imgBall.image=[UIImage imageNamed:[NSString stringWithFormat:@"lot_%d",self.lotteryIndex]];
        cell.nameLbl.text=[[list objectAtIndex:1] objectForKey:@"text"];//彩票名称
        NSString * winNums =[[list objectAtIndex:self.issueIndex*7+4] objectForKey:@"text"];//开奖号码
        NSArray  * winningNum= [winNums componentsSeparatedByString:@","];
        for (int i=0; i<winningNum.count; i++) {
            UIImageView *imgCell=[[UIImageView alloc]initWithFrame:CGRectMake(6+i*28, 0, 26, 26)];
            [imgCell setClipsToBounds:YES];
            [imgCell setContentMode:UIViewContentModeScaleAspectFit];
            imgCell.image=[UIImage imageNamed:@"ball_red"];
            
            UILabel *winNumLbl=[[UILabel alloc] initWithFrame:CGRectMake(6+i*28, 0, 26, 26)];
            winNumLbl.text=[winningNum objectAtIndex:i];
            winNumLbl.textColor=[UIColor whiteColor];
            winNumLbl.backgroundColor=[UIColor clearColor];
            winNumLbl.textAlignment=NSTextAlignmentCenter;
            winNumLbl.font=[UIFont systemFontOfSize:18];
            
            [cell.cusView addSubview:imgCell];
            [cell.cusView addSubview:winNumLbl];
        }
        [cell.btnSend addTarget:self action:@selector(PressWeb:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnSend setTitle:[[list objectAtIndex:list.count-2] objectForKey:@"text"] forState:UIControlStateNormal];
        NSString *rollOver=[[list objectAtIndex:self.issueIndex*7+5] objectForKey:@"text"];//奖池滚存
        cell.rolloverLbl.text=[[NSString alloc] initWithFormat:@"奖池滚存：%@",rollOver];;
        cell.detailLbl.text=@"";
//        cell.clickLbl.text=@"立即投注";
        
        NSString *issueNum=[[list objectAtIndex:self.issueIndex*7+2] objectForKey:@"name"];//彩票期号
        issueNum =[issueNum stringByReplacingOccurrencesOfString:@"彩票期号" withString:@"期"];
        cell.issueNumberLbl.text=issueNum;
        NSString *drawDate=[[list objectAtIndex:self.issueIndex*7+3] objectForKey:@"text"];//开奖日期
        cell.dateLbl.text=[self DateFormat:drawDate];
        return cell;
    }
    else{
        static NSString *CustomCellIdentifier=@"AwardLevelCell";
        AwardLevelCell *cell=(AwardLevelCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
        if (cell==nil) {
            cell=[[[NSBundle mainBundle] loadNibNamed:CustomCellIdentifier owner:self options:nil] objectAtIndex:0];
        }
        if (row==1) {
            cell.awardLevel.text=@"奖项";
            cell.awardNum.text=@"中奖注数";
            cell.awardMoney.text=@"奖金";
        }
        else
        {
            NSArray *awardDetail=[self GetAwardDetail:row];
            if (awardDetail.count>2) {
                cell.awardLevel.text=[awardDetail objectAtIndex:0];
                cell.awardNum.text=[awardDetail objectAtIndex:2];
                cell.awardMoney.text=[awardDetail objectAtIndex:1];
            }
            else{
                cell.awardLevel.text=@"-";
                cell.awardNum.text=@"-";
                cell.awardMoney.text=@"-";
            }
        }
        return cell;
    }
}
-(void)PressWeb:(UIButton *)button{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:button.currentTitle]];
    //    NSLog(@"%@",button.currentTitle);
    WebSaleCaiController *webCon=[[WebSaleCaiController alloc]init];
    webCon.requestUrl=request;
    [self.navigationController pushViewController:webCon animated:YES];
}
-(NSArray *)GetAwardDetail:(NSUInteger)Index{
    NSArray *renArray=[[NSArray alloc] init];
    
    NSArray *list=[[[doubleBalls objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"];
    NSString *awards=[[list objectAtIndex:self.issueIndex*7+8] objectForKey:@"text"];
    NSArray *awardItems=[awards componentsSeparatedByString:@"><"];
    
    if (Index==2) {
        NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"<"];//截取掉“<”
        NSString *trimmedString = [[awardItems objectAtIndex:Index-2] stringByTrimmingCharactersInSet:set];
        renArray=[trimmedString componentsSeparatedByString:@";"];
    }
    else if (Index==awardItems.count+1){
        NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@">"];//截取掉“>”
        NSString *trimmedString = [[awardItems objectAtIndex:Index-2] stringByTrimmingCharactersInSet:set];
        renArray=[trimmedString componentsSeparatedByString:@";"];
    }
    else
    {
        renArray=[[awardItems objectAtIndex:Index-2] componentsSeparatedByString:@";"];
    }
    return renArray;
}

#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUInteger row=[indexPath row];
    if (row==0) {
        return 138;
    }else{
        return 41;
    }
}

//日期格式转换
-(NSString *)DateFormat:(NSString *)Date{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate* inputDate = [inputFormatter dateFromString:Date];
    
    NSDateFormatter *outputFormatter =[[NSDateFormatter alloc] init];
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:@"MM月dd日"];
    NSString *strDate = [outputFormatter stringFromDate:inputDate];
    return strDate;
}
@end
